package aries;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import java.util.ResourceBundle;

public class Locales {

    /**
     * Your locales parameter.
     */
    private String locales;

    /**
     * Constructor for locales.
     *
     * @param locales
     */
    public Locales(String locales) {
        this.locales = locales;
    }

    /**
     * Get text from specified locales.
     *
     * @param key
     * @return text
     */
    public String getText(String key) {
        return ResourceBundle.getBundle(locales).getString(key);
    }
    
    /**
     * Get text from specified locales with some replaced string.
     * The replace string must be {0}.
     * 
     * @param key
     * @param replace
     * @param replaced
     * @return text
     */
    public String getText(String key, String replaced) {
        String[] theReplace = {"{0}"};
        String[] theReplaced = {replaced};
        return getText(key, theReplace, theReplaced);
    }
    
    /**
     * Get text from specified locales with some replaced string.
     * In properties files replaced text must escaped with { and }.
     * 
     * @param key
     * @param replace
     * @param replaced
     * @return text
     */
    public String getText(String key, String replace, String replaced) {
        if (replace == null || replaced == null) {
            throw new IllegalArgumentException("Aries Framework : replace or replaced can't be null.");
        }
        String[] theReplace = {"{"+replace+"}"};
        String[] theReplaced = {replaced};
        return getText(key, theReplace, theReplaced);
    }
    
    /**
     * Get text from specified locales with some replaced string.
     * The replace string must be {0}, {1}, {2}, ... and must ordered.
     * 
     * @param key
     * @param replaced
     * @return text
     */
    public String getText(String key, String[] replaced) {
        byte length = (byte) replaced.length;
        String[] replace = new String[length];
        for (byte i=0; i<length; i++) {
            replace[i] = "{"+i+"}";
        }
        return getText(key, replace, replaced);
    }
    
    /**
     * Get text from specified locales with some replaced string.
     * In properties files replaced text must escaped with { and }.
     * 
     * @param key
     * @param replace
     * @param replaced
     * @return text
     */
    public String getText(String key, String[] replace, String[] replaced) {
        String text = ResourceBundle.getBundle(locales).getString(key);
        if (replace.length != replaced.length) {
            throw new IllegalArgumentException("Aries Framework : replace length != replaced length.");
        }
        for (byte i=0; i<replace.length; i++) {
            text = text.replace(replace[i], replaced[i]);
        }
        return text;
    }

    /**
     * Get your current locales.
     *
     * @return locales
     */
    public String getLocales() {
        return locales;
    }
    
}